<?php
/**
 * Created by PhpStorm.
 * Author: Vijay  <1937832819@qq.com>
 * Date: 2019/2/28
 * Time: 10:41
 * Instructions:
 */

namespace App\Http\Controllers\Swoolex;

use App\Http\Controllers\Controller;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ChatBaseController extends Controller
{
    protected $request = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Instructions:
     * Author: Vijay  <1937832819@qq.com>
     * Time: 2019/2/28 10:50
     * @param int $status
     * @param string $msg
     * @param null $data
     * @param array $additional
     * @param array $header
     */
    public function resJson($status = 200, $msg = '', $data = null, array $additional = [], array $header = [])
    {
        $result = [
            'status' => $status,
            'msg' => $msg ? $msg : $this->statusCode($status),
            'data' => $data,
        ];
        if (count($additional) > 0) {
            foreach ($additional as $key => $val) {
                $result[$key] = $val;
            }
        }
        $result['date'] = time();
        $response = Response::create($result, 200, $header);
        throw new HttpResponseException($response);
    }

    /**
     * Instructions:
     * Author: Vijay  <1937832819@qq.com>
     * Time: 2019/2/28 10:59
     * @param $status
     * @return string
     */
    protected function statusCode($status)
    {
        switch ($status) {
            case '200':
                $text = '成功';
                break;
            case '400':
                $text = '没有登录';
                break;
            case '401':
                $text = '发生异常';
                break;
            case '402':
                $text = '系统错误';
                break;
            case '403':
                $text = '参数错误 ';
                break;
            default:
                $text = '未知错误';
        }
        return $text;
    }
}